package com.hzj.controller;


import com.hzj.pojo.SmbmsBill;
import com.hzj.pojo.SmbmsProvider;
import com.hzj.service.SmbmsBillService;
import com.hzj.service.SmbmsProviderService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hzj
 * @since 2021-04-14
 */
@Controller
public class SmbmsBillController {

    @Reference
    SmbmsBillService sbs;
    SmbmsProviderService sps;

    @GetMapping("/getAllList")
    public String getAllList(Model model) {
        List<Map<String, Object>> maps = sbs.showBillAndProvider();
        System.out.println(maps);
        model.addAttribute("BillMapList", maps);
        return "html/billlist";
    }

    @RequestMapping("/checkBillAndProvider")
    public String checkBillAndProvider(@RequestParam(required = false) Integer queryProviderId, @RequestParam(required = false) Integer queryIsPayment, @RequestParam(required = false) String queryProductName, Model model) {
        if (queryProviderId == null) {
            queryProviderId = 0;
        }
        if (queryIsPayment == null) {
            queryIsPayment = 0;
        }
        List<Map<String, Object>> maps = sbs.checkBillAndProvider(queryProviderId, queryIsPayment, queryProductName);
        model.addAttribute("BillMapList", maps);
        return "html/billlist";
    }

    @RequestMapping("/toUpdate")
    public String toupdate(int id, Model model) {
        Map<String, Object> map = sbs.getBillById(id);
        System.out.println(map);
//        List<SmbmsProvider> list = sps.list();
//        System.out.println(list);
//        model.addAttribute("spslist",list);
        model.addAttribute("smbmsBill", map);
        return "html/billmodify";
    }

    @RequestMapping("/Update")
    public String update(SmbmsBill smbmsBill) {
        System.out.println(smbmsBill);
        boolean b = sbs.updateById(smbmsBill);
        return "redirect:/getAllList";
    }

    @RequestMapping("/delete")
    public String update(int id) {
        boolean b = sbs.removeById(id);
        return "redirect:/getAllList";

    }

    @RequestMapping("/add")
    public String add(SmbmsBill smbmsBill) {
        System.out.println("新增" + smbmsBill);
        smbmsBill.setCreationdate(new Date());
        boolean save = sbs.save(smbmsBill);
        return "redirect:/getAllList";
    }

    @RequestMapping("/toadd")
    public String toadd() {
        return "html/billadd";
    }

}

